<?php
namespace Home\Controller;
use Think\Controller;
class DeliveryController extends MerchantController {

	public function _initialize(){
		parent::_initialize();	
		$this->model="delivery"; //表名
		$this->model_view="DeliveryView";
	 	$this->key="dv_id";//排序键		 
	 	$this->title_index=L('delivery');//选项卡标题	
	}

	public function index(){
		$mist_arr=array();
		$accute_arr=array('dv_oid'=>I('dv_oid'),'dv_number'=>I('dv_number'));
		$cookie_arr=array();	
		$con=searchCon($mist_arr,$accute_arr,$cookie_arr);
		$con['order_mid']=$this->merchant_info['mh_id'];	
		parent::home($con);
	}

	/**
	*上传单个运单号
	*/
	public function addHandle(){
		$list=D("OrdersView")->where(array("order_id"=>I("dv_oid")))->find();
		$hash_info=hash("sha256",$this->merchant_info["mh_id"].$list['site_id'].I("dv_oid").I("dv_tk_id").I("dv_number").$list['site_key']);
		A("Openapi/Tracking")->upload($this->merchant_info['mh_id'],
		$list['site_id'],I("dv_oid"),I("dv_tk_id"),I("dv_number"),$hash_info);
	}

	//批量上传运单号
	public function bulkDelivery(){
		$this->assign("main_title",$this->title_index);
		
		$result=parent::uploadfile($exts=array('csv'));
		if($result['status']==0)
			$this->error(L('invalid_uploading'));
				
		//dump($result);
		 $file="./Public/Uploads/".$result['info']['csv']['savepath'].$result['info']['csv']['savename'];
		$obj=new \mnshankar\CSV\CSV();
		$arr=$obj->with($file)->toArray();
		//dump($arr);exit;
		$success_count=0;
		$error_count=0;
		
		foreach($arr as $value){
		$list=D("OrdersView")->where(array("order_id"=>$value["Order ID"]))->find();
		$hash_info=hash("sha256",$this->merchant_info["mh_id"].$list['site_id'].$value["Order ID"].$value["Delivery company"].$value["Tracking number"].$list['site_key']);

		
		//$mid="",$site_id="",$oid="",$tracking_company="",$tracking_number="",$hash_info=""
		$post["mid"]=$this->merchant_info['mh_id'];
		$post["site_id"]=$list['site_id'];
		$post["oid"]=$value['Order ID'];
		$post["tracking_company"]=$value['Delivery company'];
		$post["tracking_number"]=$value['Tracking number'];
		$post["hash_info"]=$hash_info;
		//dump($post);exit;
		
		$asynhandle=new \Common\Common\AsynHandle();
		$response=$asynhandle->Get(U("Openapi/Tracking/upload@".$_SERVER['SERVER_NAME']),"",$post);
		//echo $response;
		$res_arr=json_decode($response,true);
		//dump($res_arr);exit;
		
		if($res_arr['status'])
			$success_count++;
		else
			$error_count++;
		}
		
		$this->assign("success_count",$success_count);
		$this->assign("error_count",$error_count);
		 unlink($csv);//删除文件
		 $this->display("result");
	}
		

	public function details(){
		$con['nt_isactive']=1;	
		$con['nt_id']=I("path.2");
		parent::details($con);	
	}
		

}

